package com.example.cloud.controller;

import com.example.cloud.entities.Pay;
import com.example.cloud.entities.PayDTO;
import com.example.cloud.resp.ResultData;
import com.example.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Yun
 * @Date: 2024/3/17
 */
@RestController
@Slf4j
@RequestMapping("/pay")
@Tag(name = "支付微服务模块", description = "支付CRUD")
public class PayController {
    @Resource
    private PayService payService;

    @PostMapping("/insert")
    @Operation(summary = "新增", description = "新增支付流水方法,参数格式为json")
    public ResultData<String> insertPay(@RequestBody Pay pay) {
        System.out.println(pay.toString());
        int insert = payService.insert(pay);
        return ResultData.success("成功插入记录,返回值: " + insert);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除", description = "删除流水记录方法")
    public ResultData<String> deletePayById(@PathVariable("id") Integer id) {
        System.out.println(id);
        int delete = payService.delete(id);
        return ResultData.success("成功删除记录,返回值: " + delete);
    }


    @PutMapping("/update")
    @Operation(summary = "修改", description = "修改支付流水方法")
    public ResultData<Integer> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        // 将payDTO的属性拷贝到pay中
        BeanUtils.copyProperties(payDTO, pay);

        int update = payService.update(pay);
        return ResultData.success(update);
    }

    @GetMapping("/select/{id}")
    @Operation(summary = "根据id查询流水", description = "查询支付流水的方法")
    public ResultData<Pay> selectPayById(@PathVariable("id") Integer id) {
        if (id <= 0) throw new RuntimeException("id不能为负数");
        System.out.println(id);
        Pay pay = payService.selectById(id);
        return ResultData.success(pay);
    }


    @GetMapping("/selectAll")
    @Operation(summary = "查询所有流水", description = "查询支付所有流水的方法")
    public ResultData<List<Pay>> selectAllPays() {
        List<Pay> pays = payService.selectAll();
        return ResultData.success(pays);
    }

    @Value("${server.port}")
    private String port;

    @GetMapping("/getInfo")
    public String getInfo(@Value("${test.info}") String testInfo) {
        return "testInfo: " + testInfo + "\t" + "port: " + port;
    }
}
